﻿#include "NPCInfo.h"
#include "../../Shared/Functions/Functions.h"

using namespace Server::MirEnvir;

namespace Server::MirDatabase
{

    Envir *NPCInfo::getEditEnvir()
    {
        return Envir::getEdit();
    }

    NPCInfo::NPCInfo()
    {
    }

    NPCInfo::NPCInfo(BinaryReader *reader)
    {
        Index = reader->ReadInt32();
        MapIndex = reader->ReadInt32();

        int count = reader->ReadInt32();
        for (int i = 0; i < count; i++)
        {
            CollectQuestIndexes.push_back(reader->ReadInt32());
        }

        count = reader->ReadInt32();
        for (int i = 0; i < count; i++)
        {
            FinishQuestIndexes.push_back(reader->ReadInt32());
        }

        FileName = reader->ReadString();
        Name = reader->ReadString();

        Location = new Point(reader->ReadInt32(), reader->ReadInt32());

        if (Envir::LoadVersion >= 72)
        {
            Image = reader->ReadUInt16();
        }
        else
        {
            Image = reader->ReadByte();
        }

        Rate = reader->ReadUInt16();

        if (Envir::LoadVersion >= 64)
        {
            TimeVisible = reader->ReadBoolean();
            HourStart = reader->ReadByte();
            MinuteStart = reader->ReadByte();
            HourEnd = reader->ReadByte();
            MinuteEnd = reader->ReadByte();
            MinLev = reader->ReadInt16();
            MaxLev = reader->ReadInt16();
            DayofWeek = reader->ReadString();
            ClassRequired = reader->ReadString();
            if (Envir::LoadVersion >= 66)
            {
                Conquest = reader->ReadInt32();
            }
            else
            {
                Sabuk = reader->ReadBoolean();
            }
            FlagNeeded = reader->ReadInt32();
        }
    }

    void NPCInfo::Save(BinaryWriter *writer)
    {
        writer->Write(Index);
        writer->Write(MapIndex);

        writer->Write(CollectQuestIndexes.size()());
        for (int i = 0; i < CollectQuestIndexes.size(); i++)
        {
            writer->Write(CollectQuestIndexes[i]);
        }

        writer->Write(FinishQuestIndexes.size()());
        for (int i = 0; i < FinishQuestIndexes.size(); i++)
        {
            writer->Write(FinishQuestIndexes[i]);
        }

        writer->Write(FileName);
        writer->Write(Name);

        writer->Write(Location->X);
        writer->Write(Location->Y);
        writer->Write(Image);
        writer->Write(Rate);

        writer->Write(TimeVisible);
        writer->Write(HourStart);
        writer->Write(MinuteStart);
        writer->Write(HourEnd);
        writer->Write(MinuteEnd);
        writer->Write(MinLev);
        writer->Write(MaxLev);
        writer->Write(DayofWeek);
        writer->Write(ClassRequired);
        writer->Write(Conquest);
        writer->Write(FlagNeeded);
    }

    void NPCInfo::FromText(const std::string &text)
    {
        std::vector<std::string> data = text.Split({','}, StringSplitOptions::RemoveEmptyEntries);

        if (data.size() < 6)
        {
            return;
        }

        NPCInfo *info = new NPCInfo();
        info->Name = data[0];

        int x, y;

        info->FileName = data[0];
        info->MapIndex = getEditEnvir()->MapInfoList.Where([&] (std::any d)
        {
            return d->FileName == data[1];
        }).FirstOrDefault().Index;

        if (!int::TryParse(data[2], x))
        {
            delete info;
            return;
        }
        if (!int::TryParse(data[3], y))
        {
            delete info;
            return;
        }

        info->Location = new Point(x, y);

        info->Name = data[4];

        if (!unsigned short::TryParse(data[5], info->Image))
        {
            delete info;
            return;
        }
        if (!unsigned short::TryParse(data[6], info->Rate))
        {
            delete info;
            return;
        }

        setEditEnvir(getEditEnvir() + 1);
        info->Index = getEditEnvir()->NPCIndex;
        getEditEnvir()->NPCInfoList.push_back(info);

//C# TO C++ CONVERTER TODO TASK: A 'delete info' statement was not added since info was passed to a method or constructor. Handle memory management manually.
    }

    std::string NPCInfo::ToText()
    {
        return std::string::Format("{0},{1},{2},{3},{4},{5},{6}", FileName, getEditEnvir()->MapInfoList.Where([&] (std::any d)
        {
            return d->Index == MapIndex;
        }).FirstOrDefault().FileName, Location->X, Location->Y, Name, Image, Rate);
    }

    std::string NPCInfo::ToString()
    {
        return StringHelper::formatSimple("{0}:   {1}", FileName, Functions::PointToString(Location));
    }
}
